var express = require("express");
var router = express.Router();
var db = require("../db/mysql.js");
var uuid = require('node-uuid');

const {
    SuccessModel,
    ErrorModel
} = require('../model/resModel')
/**
 * @数据库表
 * @my_project
 */
// 查询列表接口
router.get("/", function (req, res, next) {
    // 查询实例
    db.query("select * from my_project", function (err, results) {
        if (err) {
            res.send(new ErrorModel(results));
        } else {
            res.send(new SuccessModel(results, "查询成功"));
        }
    });
});

// 新增接口
router.post("/", function (req, res, next) {
    var uidv4 = uuid.v4();
    let {
        projectName
    } = req.query;
    // 查询实例
    db.query(`INSERT INTO my_project(project_name, project_id) VALUES(${projectName}, ${uidv4})`, (err, results) => {
        if (err) {
            res.send(new ErrorModel(results));
        } else {
            res.send(new SuccessModel(results, "新增成功"));
        }
    });
});
// 修改
router.put("/", function (req, res, next) {
    // 查询实例
    db.query("select * from my_project", [], function (result) {

        res.send(1);
    });
});

// 删除
router.delete("/", function (req, res, next) {
    console.log("debug log --> ", req.query.projectId)

    查询实例
    db.query(`DELETE FROM  my_project WHERE  project_id=${req.query.projectId}`, function (err, results) {
        if (err) {
            res.send(new ErrorModel(results));
        } else {
            res.send(new SuccessModel(results, "删除成功"));
        }
    });
});

module.exports = router;